package cn.wx.read;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.csvreader.CsvReader;

import cn.wx.common.utils.JSONUtils;

public class ExpertCoutryLanRead {
	
	public static void main(String[] args) throws Exception {
		
		Map<String,String[]> cls = new HashMap<>();
		
		/**
		 * 读取领域信息
		 */
		File file1 = new File("E:\\my_read_csv\\org\\affiliation.csv");
        FileInputStream fileInputStream1 = new FileInputStream(file1);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
		CsvReader csvReader1 = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader1.readHeaders();
		while (csvReader1.readRecord()){
			/**
			 * 解析csv
			 */
			String orgName = csvReader1.get(1);
			String country = csvReader1.get(2);
			String language = csvReader1.get(3);
			cls.put(orgName, new String[]{country,language});
		}
		
		File file = new File("E:\\my_read_csv\\org\\expert_authorsOrg.csv");
        FileInputStream fileInputStream = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
		
		CsvReader csvReader = new CsvReader(bufferedInputStream,Charset.forName("utf-8"));
		StringBuffer sb = null;
		BufferedWriter output = null;
		// 读表头
        csvReader.readHeaders();
        
        
        File of = new File("E:\\my_read_csv\\org\\expert_authorsOrg_error.csv");
		FileOutputStream ofs = new FileOutputStream(of,true);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);
		
		sb = new StringBuffer();
		sb.append(JSONUtils.c("fId"))
		.append(",").append(JSONUtils.c("authorsOrg"))
		.append(",").append(JSONUtils.c("country"))
		.append(",").append(JSONUtils.c("language"))
		.append(",").append(JSONUtils.c("countryError"))
		.append(",").append(JSONUtils.c("languageError"));
		
		output.write(sb.toString());
		output.newLine();
        
		int i = 0;
        while (csvReader.readRecord()){
        	
            // 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取" + (i++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}

			/**
			 * 解析csv
			 * "fId","country","language","authorsOrg"
			 */
			String fId = csvReader.get(0);
			String country = csvReader.get(1);
			String language = csvReader.get(2);
			String authorsOrg = csvReader.get(3);
			
			if(StringUtils.isNotBlank(authorsOrg)) {
				
				
				
				String[] cl = cls.get(authorsOrg);
				
				
				if(cl == null || !cl[0].equals(country) || !cl[1].equals(language)) {
					
					if(cl == null) {
						sb = new StringBuffer();
						sb.append(fId).append(",")
						.append(JSONUtils.c(authorsOrg)).append(",")
						.append(JSONUtils.c("")).append(",")
						.append(JSONUtils.c("")).append(",")
						.append(JSONUtils.c(country)).append(",")
						.append(JSONUtils.c(language));
					}else{
						
						String c1 = cl[0];
						
						if("Hong Kong".equals(c1)) {
							c1 = "Hong Kong China";
						}
						
						else if("Taiwan".equals(c1)) {
							c1 = "Taiwan China";
						}
						
						else if("Macau".equals(c1)) {
							c1 = "Macau China";
						}
						
						else if("Maocau".equals(c1)) {
							c1 = "Maocau China";
						}
						
						sb = new StringBuffer();
						sb.append(fId).append(",")
						.append(JSONUtils.c(authorsOrg)).append(",")
						.append(JSONUtils.c(c1)).append(",")
						.append(JSONUtils.c(cl[1])).append(",")
						.append(JSONUtils.c(country)).append(",")
						.append(JSONUtils.c(language));
					}
					
					output.write(sb.toString());
					output.newLine();
					
				}
				output.flush();
			}

        }
		
        output.flush();
        output.close();
        output = null;
        
        /**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
		
		
	}
	
}
